set -e

pwd
# 蒲公英脚本
curl --insecure https://raw.githubusercontent.com/Pgyer/TravisFile/master/pgyer_upload.sh -o pgyer_upload.sh
chmod +x pgyer_upload.sh

# 为脚本赋权
chmod 755 ./sh/*.sh
chmod 777 ./config.xml

# 判断是否有传入版本，如无传入，默认为alpha
LIBRARY=$1
NEWVERSION=$2

# 提取上一次构建的版本号
WIDGET=$(grep 'eegrid' ./config.xml)
ID=$(echo $WIDGET|sed 's/<widget id="com.eegrid.efos.//g')

if [ ${ID:0:7} == app.alp ]
then
    LASTID=app.alpha
elif [ ${ID:0:7} == app.bet ]
then 
    LASTID=app.beta
else
    LASTID=app
fi

VERSION=$(echo $WIDGET|sed 's/<widget id="com.eegrid.efos.'$LASTID'" version="//g')
echo "默认上次版本号：${VERSION:0:5}"

if [ $NEWVERSION ]
then
    echo "有传入版本号，版本号为$NEWVERSION"
    THISVERSION=$NEWVERSION 
else
    echo "无传入版本号，使用默认上一次版本号"
    THISVERSION=${VERSION:0:5}
fi

if [ $LIBRARY ]
then
  echo "传入参数字符串不为空,参数为$LIBRARY"
else
  echo "传入参数字符串为空，转换为alpha"
  LIBRARY=alpha
fi

if [ $LIBRARY == alpha ]
then
    MAINACTIVITYSRC='app/alpha'
    PRODUCTION='0zDBBoitw6Un8kR5zPmKZ1XlSgTx4ksvOXqog'
    STAGING='jH0mcUdm0kzlmVMCfaQskiaSPatq4ksvOXqog'
    JPUSHAPPKEY=225df88f3fd915276626d3aa
    FILESERVICE=http://192.168.1.31:9998
    APISERVICE=http://192.168.1.22:8899
    THISID=app.alpha
    APPNAME=efos-alpha-$THISVERSION

elif [ $LIBRARY == beta ] 
then
    MAINACTIVITYSRC='app/beta'
    PRODUCTION='RFrs34yf8ieTnMeN7tEAAt4xPgrI4ksvOXqog'
    STAGING='T4Lh41UbptUAn81mJNb3CDPJHch14ksvOXqog'
    JPUSHAPPKEY=af5debcb5a10be8acf4fa7a9
    FILESERVICE=http://file.eegrid.com
    APISERVICE=http://test.api.eegrid.com
    THISID=app.beta
    APPNAME=efos-beta-$THISVERSION
else
    MAINACTIVITYSRC='app'
    PRODUCTION='9Rp3Xd2reAnSSbBTrKPXzSXHefdE4ksvOXqog'
    STAGING='B1oStLOhZpVss52H9Md3u9cSBJqC4ksvOXqog'
    JPUSHAPPKEY=8124f747acdcb495b6433d76
    FILESERVICE=http://file.eegrid.com
    APISERVICE=http://clientapi.eegrid.com
    THISID=app
    APPNAME=efos-prod-$THISVERSION
fi

# 更新config.xml版本和版本号
    sed -ig -e '2 c\ 
<widget id="com.eegrid.efos.'$THISID'" version="'$THISVERSION'" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
' ./config.xml

# 修改config.xml code-push key
codepushline=$(grep -n 'CodePushDeploymentKey' ./config.xml | head -1  | cut  -d  ":"  -f  1)
sed -ig -e "$codepushline c\ 
     <preference name='CodePushDeploymentKey' value='$PRODUCTION' />
    " ./config.xml

# 安装依赖并打包
rm -f package-lock.json
npm install

# 修改code-push-key
echo "修改codepushkey:$PRODUCTION,$STAGING"
sed -ig -e "14 c\ 
    Production: '$PRODUCTION',
    " -e "15 c\ 
    Staging: '$STAGING'
    " ./src/app/codepush.constants.ts

# 修改config.ts
echo "修改config.ts"
fileService=$(grep -n 'serviceAddr' ./src/app/services/config.ts | head -1  | cut  -d  ":"  -f  1)
apiService=$(grep -n 'serviceAddr' ./src/app/services/config.ts | tail -1  | cut  -d  ":"  -f  1)

 sed -ig -e "$fileService c\ 
    'serviceAddr': '$FILESERVICE',
    " -e "$apiService c\ 
'serviceAddr': '$APISERVICE',
" ./src/app/services/config.ts

ionic cordova plugin rm jpush-phonegap-plugin  --variable APP_KEY=225df88f3fd915276626d3aa
ionic cordova plugin add jpush-phonegap-plugin --variable APP_KEY=$JPUSHAPPKEY

ionic cordova platform rm android
ionic cordova platform add android@7.1.4 --nofetch --no-resources

# 修改平台--MainActivity.java
echo '修改平台文件--MainActivity.java'
sed -ig -e "23 a\ 
import android.os.Build;
" -e "23 a\ 
import android.view.View;
" -e "29 a\ 
getWindow().getDecorView().setSystemUiVisibility(
" -e "29 a\ 
View.SYSTEM_UI_FLAG_FULLSCREEN| View.SYSTEM_UI_FLAG_LAYOUT_STABLE| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_IMMERSIVE
" -e "29 a\ 
  );
" ./platforms/android/app/src/main/java/com/eegrid/efos/$MAINACTIVITYSRC/MainActivity.java

#修改平台--AndroidManifest.xml
echo '修改平台文件--AndroidManifest.xml'
sed -ig -e 's/package="com.eegrid/android:resizeableActivity="true" package="com.eegrid/
' -e '5 a\ 
<meta-data android:name="android.max_aspect" android:value="2.5" />
' -e '5 a\ 
<meta-data android:name="android.notch_support" android:value="true" />
' ./platforms/android/app/src/main/AndroidManifest.xml


#修改图标和起始页
cp ./sh/icon/$LIBRARY.png ./resources/icon.png
ionic cordova resources

#build android
ionic cordova build android --prod --release --no-resources

# 签名
    cp ./sh/ionic4travis.jks ./platforms/android/app/build/outputs/apk/release/ionic4travis.jks
    cd ./platforms/android/app/build/outputs/apk/release/
    jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore ./ionic4travis.jks -storepass ionic4travis -keypass ionic4travis app-release-unsigned.apk ionic4travis
    jarsigner -verify app-release-unsigned.apk


# 压缩优化
/Users/nxt/Library/Android/sdk/build-tools/28.0.3/zipalign -v 4 app-release-unsigned.apk $APPNAME.apk

if [ $NEWVERSION ]
then
echo "打包构建成功，该版本为$LIBRARY,版本号为$NEWVERSION"
else
echo "打包构建成功，该版本为$LIBRARY,版本号为${VERSION:0:5}"
fi

PGYER_APIKEY=b89de6059e9864b40fec524f155bb3a9
# 上传至蒲公英
cd ../../../../../../../
./pgyer_upload.sh ./platforms/android/app/build/outputs/apk/release/$APPNAME.apk $PGYER_APIKEY